import { RouteRecordRaw, createRouter, createWebHistory } from 'vue-router'

import PortalLayout from '@/layouts/PortalLayout.vue'
import notFound404 from '@/views/notFound404.vue'
import useUserStore from '@/store/modules/user'

import { npsStart, npsDone } from '@/utils/nprogress'
import 'nprogress/nprogress.css'

// 固定路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/',
    component: PortalLayout,
    redirect: '/home',
    name: 'HomeLayout',
    meta: { name: '概览', icon: 'DataLine', show: true },
    children: [
      {
        path: '/home',
        component: () => import('@/views/Home.vue'),
        name: 'Home',
        meta: { name: '首页', icon: 'HomeFilled', show: true }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/Login.vue'),
    name: 'Login'
  },
  {
    path: '/screen',
    component: () => import('@/views/screen/Screen.vue'),
    meta: { name: '数据大屏', icon: 'Monitor', show: true, target: '_blank' },
    name: 'Screen'
  },
  {
    path: '/system',
    component: PortalLayout,
    redirect: '/system/user',
    name: 'System',
    meta: { name: '系统管理', icon: 'Setting', show: true },
    children: [
      {
        path: '/system/user',
        component: () => import('@/views/system/User.vue'),
        meta: { name: '用户管理', icon: 'UserFilled', show: true },
        name: 'User'
      },
      {
        path: '/system/role',
        component: () => import('@/views/system/Role.vue'),
        meta: { name: '角色管理', icon: 'Avatar', show: true },
        name: 'Role'
      },
      {
        path: '/system/perm',
        component: () => import('@/views/system/Permission.vue'),
        meta: { name: '权限管理', icon: 'Lock', show: true },
        name: 'Permission'
      }
    ]
  },
  {
    path: '/goods',
    component: PortalLayout,
    redirect: '/goods/logo',
    name: 'Goods',
    meta: { name: '商品管理', icon: 'GoodsFilled', show: true },
    children: [
      {
        path: '/goods/logo',
        component: () => import('@/views/goods/Logo.vue'),
        meta: { name: '品牌管理', icon: 'Aim', show: true },
        name: 'Logo'
      },
      {
        path: '/goods/attr',
        component: () => import('@/views/goods/Attr.vue'),
        meta: { name: '属性管理', icon: 'Link', show: true },
        name: 'Attr'
      },
      {
        path: '/goods/sku',
        component: () => import('@/views/goods/Sku.vue'),
        meta: { name: 'SKU管理', icon: 'Goods', show: true },
        name: 'Sku'
      },
      {
        path: '/goods/spu',
        component: () => import('@/views/goods/Spu.vue'),
        meta: { name: 'SPU管理', icon: 'ReadingLamp', show: true },
        name: 'Spu'
      }
    ]
  },
  {
    path: '/example',
    component: PortalLayout,
    name: 'Example',
    meta: { name: '组件示例', icon: 'HomeFilled', show: true },
    children: [
      {
        path: '/example/apitable',
        component: () => import('@/views/example/ApiTable.vue'),
        name: 'ApiTable',
        meta: { name: '数据表格', icon: 'List', show: true }
      },
      {
        path: '/example/table',
        component: () => import('@/views/example/Table.vue'),
        name: 'ETable',
        meta: { name: '静态表格', icon: 'List', show: true }
      }
    ]
  },
  {
    path: '/404',
    component: notFound404,
    name: '404'
  },
  {
    path: '/:pathMatch(.*)*',
    component: notFound404,
    name: 'notFound404'
  }
]
const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  npsStart()
  const userStore = useUserStore()
  const token = userStore.token || ''
  // console.log('to.from', from.path)
  // console.log('to.path', to.path)
  if (token) {
    if (to.path == '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    // 这里判断在login页就不跳login了，可能无限循环
    if (to.path == '/login') {
      next()
    } else {
      next({ path: '/login', query: { redirect: to.path } })
    }
  }
  // next()
})
router.afterEach(() => {
  npsDone()
})

export default router
